package com.luocc.ccdemo

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.luocc.ccdemo.interceptor.AddCookieInterceptor
import com.luocc.ccdemo.interceptor.LogInterceptor
import com.luocc.ccdemo.interceptor.SaveCookieInterceptor
import com.luocc.ccdemo.utils.OkHttpUtil
import okhttp3.Interceptor

/**
 * 记录：
 * 待办事项
 * 博客，里面有广场和公众号和体系？
 *
 */
class MainActivity : AppCompatActivity() {
    private var mFragmentId: Int = -1
    private var mCount = 0
    private val mAllPage: IntArray =
        intArrayOf(R.id.fragment_home, R.id.fragment_mine, R.id.fragment_video_zone)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_container) as NavHostFragment
        val navController = navHostFragment.navController
        val bottomNavigation = findViewById<BottomNavigationView>(R.id.bottom_navigation)
        bottomNavigation.setupWithNavController(navController)

        navController.addOnDestinationChangedListener { _, destination, _ ->
            if (destination.id in mAllPage) {
                bottomNavigation.visibility = View.VISIBLE
            } else {
                bottomNavigation.visibility = View.GONE
            }
            mFragmentId = destination.id
            mCount = 0
        }

    }

    override fun onBackPressed() {
        if (mFragmentId in mAllPage) {
            mCount++
            if (mCount > 1) {
                finish()
            }
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show()
        } else {
            super.onBackPressed()
        }
    }
}